import Vue from 'vue';
import Vuex from 'vuex';
import Axios from 'axios';
import MutationType from './mutationType';

Vue.use(Vuex);

export default new Vuex.Store({
    // 自定义共享状态
    state: {
        tabbarState: true,
        comingSoonListState: []
    },
    // state状态过滤
    getters: {
        comingSoonListGetter(state) {
            return state.comingSoonListState.filter((item, index) => index < 3);
        }
    },
    // 唯一修改状态的位置，可以被devtools工具监控
    mutations: {
        tabbarMutation(state, data) {
            state.tabbarState = data;
        },
        // 动态配置mutation的名字
        [MutationType.GET_COMINGSONG_MUTATION](state, data) {
            state.comingSoonListState = data;
        }
    },
    // 处理异步请求
    actions: {
        getComingSongAction(store) {
            Axios({
                url: 'https://m.maizuo.com/gateway?cityId=370100&pageNum=1&pageSize=10&type=2&k=9149434',
                method: 'GET',
                headers: {
                    'X-Client-Info': '{"a":"3000","ch":"1002","v":"5.0.4","e":"159668407331181462568961","bc":"370100"}',
                    'X-Host': 'mall.film-ticket.film.list'
                }
            }).then(res => {
                store.commit(MutationType.GET_COMINGSONG_MUTATION, res.data.data.films);
            });
        }
    },
    modules: {}
});
